<template>
  <div class="my-container">
    <!-- 已登录头部 -->
    <div v-if="user" class="header user-info">
      <div class="base-info">
        <div class="left">
          <van-image
            class="avatar"
            round
            fit="cover"
            :src="userInfo.photo"
          />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button size="mini" round to="/user/profile"> 编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- /已登录头部 -->

    <!-- 未登录头部 -->
    <div v-else class="header not-login">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile" src="@/assets/mobile.png" alt="" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <!-- /未登录头部 -->

    <!-- 宫格导航 -->
    <!-- column-num自定义列数 -->
    <van-grid class="grid-nav" column-num="2" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- /宫格导航 -->

    <van-cell title="消息通知" is-link />
    <van-cell class="mb-9" title="小智同学" is-link />
    <van-cell
      v-if="user"
      clickable
      class="logout-cell"
      title="退出登录"
      @click="onLogout"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'

export default {
  name: 'MyIndex',

  data() {
    return {
      // 用户信息
      userInfo: {},
    }
  },

  mounted() {},
  computed: {
    ...mapState(['user']),
  },
  created() {
    //如果用户登陆了，则请求加载用户信息数据
    if (this.user) {
      this.loadUserInfo()
    }
  },
  methods: {
    // 退出登录
    onLogout() {
      // 1.退出提示，是否退出登录
      // 在组件中 要使用this.$dialog 来调用弹框组件
      this.$dialog
        .confirm({
          title: '确认退出吗？',
        })
        .then(() => {
          // on confirm
          // console.log('确认执行这里');
          // 确认退出 清除登录状态（容器中的user +本地存储中的user 要删除）
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
          // console.log('取消执行这里');
        })
      // 2.未登录头部显示 已登录头部隐藏 以及退出登录隐藏
      // 3.我的  变为 未登录
    },
    //获取指定用户信息
    async loadUserInfo() {
      try {
        const { data } = await getUserInfo()
        // console.log(data);
         this.userInfo= data.data
      } catch (err) {
        this.$toast('获取用户信息失败，请稍后重试')
      }
    },
  },
}
</script>

<style lang="less" scoped>
.my-container {
  .header {
    height: 361px;
    background: url('@/assets/banner.png');
    // background-size: cover把背景图扩展至足够大，直至完全覆盖背景区域，图片比例保持不变且不会失真，但某些部分被切割无法显示完整背景图像
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column; //默认主轴为水平 设置为垂直 垂直排列
      justify-content: center;
      align-items: center;
      .mobile {
        height: 132px;
        width: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      height: 231px;
      box-sizing: border-box; //盒子模型 padding不会撑大盒子
      padding: 76px 32px 23px;
      display: flex;
      justify-content: space-between; //撑开到最两边
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          margin-right: 23px;
          border: 3px solid #fff;
        }
      }
      .name {
        font-size: 30px;
        color: #fff;
      }
    }
    .data-stats {
      // height: 131px;
      display: flex;
      .data-item {
        flex: 1; //所有孩子平分父亲宽度
        height: 131px;
        display: flex;
        flex-direction: column; //设置主轴为y轴
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    height: 145px;
    i.iconfont {
      font-size: 45px;
    }
    i.icon-shoucang {
      color: #eb5253;
    }
    i.icon-lishi {
      color: #ff9d1d;
    }
    span.text {
      font-size: 25px;
    }
  }
  .logout-cell {
    color: red;
    text-align: center;
  }
}
</style>
